package com.tueye.web.controller.usermanager;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tueye.entity.user.User;
import com.tueye.service.usermanager.IRegisterService;
import com.tueye.utils.DateUtils;
import com.tueye.utils.UUIDGenerator;
import com.tueye.utils.servlet.CaptchaServiceSingleton;
import com.tueye.web.controller.BaseController;

@SuppressWarnings("unchecked")
@org.springframework.stereotype.Controller
@RequestMapping("/register.do")
public class RegisterController extends BaseController {
	private Log log = LogFactory.getLog(RegisterController.class);
	
	@Autowired
	@Qualifier("registerService")
	private IRegisterService registerService;
 
 
	@RequestMapping(params = "action=email")
	public String email (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String Email = (String) request.getParameter("Email");
		System.out.println("%%%%%%%%%%%%%"+Email);
		Email = java.net.URLDecoder.decode(Email , "UTF-8");//解码
		List list=registerService.getUserByemail(Email);
        System.out.println("@@@@@@@@"+list.size());
		if(list.size()==0){   
			   response.getWriter().write("true");//此值jquery可以接收到   
			  }else{   
        	   response.getWriter().write("false");   
			  }   
		return null; 
		
	}
	
	@RequestMapping(params = "action=byname")
	public String byname (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String byname = (String) request.getParameter("byname");
		System.out.println("%%%%%%%%%%%%%"+byname);
		byname = java.net.URLDecoder.decode(byname , "UTF-8");//解码
		List list=registerService.getUserBybyname(byname);
        System.out.println("@@@@@@@@"+list.size());
		if(list.size()==0){   
			   response.getWriter().write("true");//此值jquery可以接收到   
			  }else{   
        	   response.getWriter().write("false");   
			  }   
		return null; 
		
	}
	@RequestMapping(params = "action=save")
	public String save (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		
		String topic_code = (String) request.getParameter("topic_code");
		boolean flag = CaptchaServiceSingleton.getInstance().validateCaptchaResponse(topic_code, request.getSession());
		if(flag == false){
			 response.getWriter().write("false"); 
			 return null;
		}
		 
		String byname = (String) request.getParameter("byname");
		String password = (String) request.getParameter("password1");
		String Email = (String) request.getParameter("Email");
		String nowHome = (String) request.getParameter("nowHome");
		 
		nowHome = java.net.URLDecoder.decode(nowHome , "UTF-8");//解码
		User user = new User();
		user.setByname(byname);
		user.setEmail(Email);
		user.setNowHome(nowHome);
		user.setPassword(password);
		String id = UUIDGenerator.getUUID();
		user.setId(id);
		user.setSys_time(DateUtils.getCurrentDate());
		registerService.saveUser(user);
	    
		response.getWriter().write("true"); 
		return null; 
		
	}

}
